﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
    /// <summary>
    /// 数据访问类:Reagent
    /// </summary>
    public partial class Reagent
    {
        ReagentState mReagentDal = new ReagentState();
        public Reagent()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLiteHelper.GetMaxID("reagentID", "Reagent");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int reagentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Reagent");
            strSql.Append(" where reagentID=@reagentID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@reagentID", DbType.Int32,4)           };
            parameters[0].Value = reagentID;

            return SQLiteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Clipper.Model.Reagent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Reagent(");
            strSql.Append("brand,DetectItem,batchNumber,peirodValidity,barcode,reagentOrder,ReagentLeft,StateId)");
            strSql.Append(" values (");
            strSql.Append("@brand,@DetectItem,@batchNumber,@peirodValidity,@barcode,@reagentOrder,@ReagentLeft,@StateId)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@brand", DbType.String,20),
                    new SQLiteParameter("@DetectItem", DbType.String,20),
                    new SQLiteParameter("@batchNumber", DbType.String,20),
                    new SQLiteParameter("@peirodValidity", DbType.DateTime),
                    new SQLiteParameter("@barcode", DbType.String,20),
                    new SQLiteParameter("@reagentOrder", DbType.Int16,1),
                    new SQLiteParameter("@ReagentLeft", DbType.Int32,4),
                    new SQLiteParameter("@StateId", DbType.Int32,4)};
            parameters[0].Value = model.brand;
            parameters[1].Value = model.DetectItem;
            parameters[2].Value = model.batchNumber;
            parameters[3].Value = model.peirodValidity;
            parameters[4].Value = model.barcode;
            parameters[5].Value = model.reagentOrder;
            parameters[6].Value = model.ReagentLeft;
            parameters[7].Value = model.StateId;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Clipper.Model.Reagent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Reagent set ");
            strSql.Append("brand=@brand,");
            strSql.Append("DetectItem=@DetectItem,");
            strSql.Append("batchNumber=@batchNumber,");
            strSql.Append("peirodValidity=@peirodValidity,");
            strSql.Append("barcode=@barcode,");
            strSql.Append("reagentOrder=@reagentOrder,");
            strSql.Append("ReagentLeft=@ReagentLeft,");
            strSql.Append("StateId=@StateId");
            strSql.Append(" where reagentID=@reagentID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@brand", DbType.String,20),
                    new SQLiteParameter("@DetectItem", DbType.String,20),
                    new SQLiteParameter("@batchNumber", DbType.String,20),
                    new SQLiteParameter("@peirodValidity", DbType.DateTime),
                    new SQLiteParameter("@barcode", DbType.String,20),
                    new SQLiteParameter("@reagentOrder", DbType.Int16,1),
                    new SQLiteParameter("@ReagentLeft", DbType.Int32,4),
                    new SQLiteParameter("@StateId", DbType.Int32,4),
                    new SQLiteParameter("@reagentID", DbType.Int32,8)};
            parameters[0].Value = model.brand;
            parameters[1].Value = model.DetectItem;
            parameters[2].Value = model.batchNumber;
            parameters[3].Value = model.peirodValidity;
            parameters[4].Value = model.barcode;
            parameters[5].Value = model.reagentOrder;
            parameters[6].Value = model.ReagentLeft;
            parameters[7].Value = model.StateId;
            parameters[8].Value = model.reagentID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateISI(Clipper.Model.Reagent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Reagent set ");
            strSql.Append("PTISIValue=@PTISIValue");
            strSql.Append(" where reagentID=@reagentID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@PTISIValue", DbType.Double),
                    new SQLiteParameter("@reagentID", DbType.Int32,8)};
            parameters[0].Value = model.PtISIValue;
            parameters[1].Value = model.reagentID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int reagentID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Reagent ");
            strSql.Append(" where reagentID=@reagentID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@reagentID", DbType.Int32,4)           };
            parameters[0].Value = reagentID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string reagentIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Reagent ");
            strSql.Append(" where reagentID in (" + reagentIDlist + ")  ");
            int rows = SQLiteHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Clipper.Model.Reagent GetModel(int reagentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reagentID,brand,DetectItem,batchNumber,peirodValidity,barcode,reagentOrder,ReagentLeft,StateId,PTISIValue from Reagent ");
            strSql.Append(" where reagentID=@reagentID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@reagentID", DbType.Int32,4)
            };
            parameters[0].Value = reagentID;

            Clipper.Model.Reagent model = new Clipper.Model.Reagent();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 通过试剂名称获得对象实体
        /// </summary>
        public Clipper.Model.Reagent GetModelByReagentName(string reagentName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reagentID,brand,DetectItem,batchNumber,peirodValidity,barcode,reagentOrder,ReagentLeft,StateId,PTISIValue from Reagent ");
            strSql.Append(" where brand=@brand");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@brand", DbType.String,20)         };
            parameters[0].Value = reagentName;

            Clipper.Model.Reagent model = new Clipper.Model.Reagent();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 通过条码获得对象实体
        /// </summary>
        public Clipper.Model.Reagent GetModelByBarcode(string barcode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reagentID,brand,DetectItem,batchNumber,peirodValidity,barcode,reagentOrder,ReagentLeft,StateId,PTISIValue from Reagent ");
            strSql.Append(" where barcode=@barcode");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@barcode", DbType.String,20)           };
            parameters[0].Value = barcode;

            Clipper.Model.Reagent model = new Clipper.Model.Reagent();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Clipper.Model.Reagent DataRowToModel(DataRow row)
        {
            Clipper.Model.Reagent model = new Clipper.Model.Reagent();
            if (row != null)
            {
                if (row["reagentID"] != null && row["reagentID"].ToString() != "")
                {
                    model.reagentID = int.Parse(row["reagentID"].ToString());
                }
                if (row["brand"] != null)
                {
                    model.brand = row["brand"].ToString();
                }
                if (row["DetectItem"] != null)
                {
                    model.DetectItem = row["DetectItem"].ToString();
                }
                if (row["batchNumber"] != null)
                {
                    model.batchNumber = row["batchNumber"].ToString();
                }
                if (row["peirodValidity"] != null && row["peirodValidity"].ToString() != "")
                {
                    model.peirodValidity = DateTime.Parse(row["peirodValidity"].ToString());
                }
                if (row["barcode"] != null)
                {
                    model.barcode = row["barcode"].ToString();
                }
                if (row["reagentOrder"] != null && row["reagentOrder"].ToString() != "")
                {
                    model.reagentOrder = int.Parse(row["reagentOrder"].ToString());
                }
                if (row["ReagentLeft"] != null && row["ReagentLeft"].ToString() != "")
                {
                    model.ReagentLeft = int.Parse(row["ReagentLeft"].ToString());
                }
                if (row["StateId"] != null && row["StateId"].ToString() != "")
                {
                    model.StateId = int.Parse(row["StateId"].ToString());
                    model.StateModel = mReagentDal.GetModel(model.StateId);
                }
                if (row["PTISIValue"] != null && row["PTISIValue"].ToString() != "")
                {
                    model.PtISIValue = double.Parse(row["PTISIValue"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reagentID,brand,DetectItem,batchNumber,peirodValidity,barcode,reagentOrder,ReagentLeft,StateId,PTISIValue ");
            strSql.Append(" FROM Reagent ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Reagent ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SQLiteHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.reagentID desc");
            }
            strSql.Append(")AS Row, T.*  from Reagent T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return SQLiteHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "Reagent";
            parameters[1].Value = "reagentID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #region  获得ReagentID
        /// <summary>
        ///  通过检测项获得剩余量大于等于一次用量的试剂ID
        /// </summary>
        /// <param name="detectItem">检测项英文名称</param>
        /// <param name="reagOrder">试剂1，还是试剂2</param>
        /// <returns>试剂ID</returns>
        public int GetReagentID(string detectItem, byte reagOrder)
        {
            int reagID = 0;
            StringBuilder strSql = new StringBuilder();
            switch (reagOrder)
            {
                case 1:
                    strSql.Append("select reagentID from Reagent inner join DetectItems on DetectItems.DetectItem=Reagent.DetectItem where Reagent.[ReagentLeft]>= detectItems.[reagentAmount1]");
                    strSql.Append("and  reagent.[reagentOrder]=1 and reagent.[DetectItem]='" + detectItem + "'");
                    break;
                case 2:
                    strSql.Append("select reagentID from Reagent inner join DetectItems on DetectItems.DetectItem=Reagent.DetectItem where Reagent.[ReagentLeft]>= detectItems.[reagentAmount2]");
                    strSql.Append("and  reagent.[reagentOrder]=2 and reagent.[DetectItem]='" + detectItem + "'");
                    break;
                case 3:
                    strSql.Append("select reagentID from Reagent inner join DetectItems on DetectItems.DetectItem=Reagent.DetectItem where Reagent.[ReagentLeft]>= detectItems.[reagentAmount3]");
                    strSql.Append("and  reagent.[reagentOrder]=3 and reagent.[DetectItem]='" + detectItem + "'");
                    break;
            }
            object ob = SQLiteHelper.GetSingle(strSql.ToString());
            if (ob != null)
            {
                reagID = Convert.ToInt32(ob);
            }

            return reagID;
        }
        #endregion // 获得ReagentID

        #endregion  ExtensionMethod
    }
}

